
#include "BarraDeFerramentas/MascaraSuavizacao.h"
#include "PetLab.h"


MascaraSuavizacao::MascaraSuavizacao(int cx, int cy, int x, int y)
    : Controle(cx, cy, x, y)
{
    Ordem(3);
}


void MascaraSuavizacao::Ordem(int n)
{
    m_ordem = n;
    
    Matriz().Retangulo(  0,   0,   Largura(),   Altura(), RGBA(  0,  0,  0,255));
    Matriz().Retangulo(  1,   1, Largura()-2, Altura()-2, RGBA(255,255,255,255));

    double dL = ((double)Largura()-1)/n;
    double dA = ((double)Altura()-1)/n;
    
    for(int i=1; i<n; i++)
        for(int j=1; j<Altura()-1; j++)
            Matriz()[ARREDONDAR(i*dL)][j] = RGBA(255,0,0,255);

    for(int i=1; i<Largura()-1; i++)
        for(int j=1; j<n; j++)
            Matriz()[i][ARREDONDAR(j*dA)] = RGBA(255,0,0,255);
}



void MascaraSuavizacao::Redimensionar(int cx, int cy)
{
    Largura(cx);
    Altura(cy);
    
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
}


bool MascaraSuavizacao::AoBaixarEsq(EventoBaixarEsq ev)
{

    MessageBox(0,"agucamento",0,0);
return false;


    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+64+32;
            int g = (int)(fator * 64/2)+128+32;
            int b = 32;
            Matriz()[i][j] = RGBA(r,g,b,128);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(255,255,0/*128+64+32*/,255);
        Matriz()[i][Altura()-1] = RGBA(255,255,0/*128+64+32*/,255);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(255,255,0/*128+64+32*/,255);
        Matriz()[Largura()-1][j] = RGBA(255,255,0/*128+64+32*/,255);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    ev.janela->Redesenhar(this);
}


bool MascaraSuavizacao::AoSubirEsq(EventoSubirEsq ev)
{
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    ev.janela->Redesenhar(this);

	return false;
}
